﻿using System;
using System.Timers;
using System.Windows;

namespace CarStatus
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow
    {
        public MainWindow()
        {
            InitializeComponent();
            _timeOfDayTimer.Elapsed += Tick;
            _delayedHideTimer.Elapsed += CheckHide;
            Tick();
            _timeOfDayTimer.Enabled = true;
        }

        private void MinimizeWindow()
        {
            WindowState = WindowState.Minimized;
        }
        private void RestoreWindow()
        {
            WindowState = WindowState.Normal;
        }

        private void WindowLoaded(object sender, RoutedEventArgs e)
        {
            GreenButton.Visibility = Visibility.Collapsed;
            RedButton.Visibility = Visibility.Visible;
        }

        private void GreenButtonClicked(object sender, RoutedEventArgs e)
        {
            ToggleRedButton();
            _delayedHideTimer.Enabled = false;
        }

        private void ToggleRedButton()
        {
            GreenButton.Visibility = Visibility.Collapsed;
            RedButton.Visibility = Visibility.Visible;
        }

        private void RedButtonClicked(object sender, RoutedEventArgs e)
        {
            ToggleGreenButton();
            _delayedHideTimer.Enabled = true;
        }

        private void ToggleGreenButton()
        {
            GreenButton.Visibility = Visibility.Visible;
            RedButton.Visibility = Visibility.Collapsed;
        }

        private readonly Timer _delayedHideTimer = new Timer(1 * 1000);
        private void CheckHide(object sender, ElapsedEventArgs e)
        {
            _delayedHideTimer.Enabled = false;
            if (GreenButton.Visibility == Visibility.Visible)
                Dispatcher.BeginInvoke(new Action(MinimizeWindow));
        }

        private readonly Timer _timeOfDayTimer = new Timer(5 * 1 * 1000);
        private void Tick(object sender, ElapsedEventArgs e)
        {
            Tick();
        }
        private void Tick()
        {
            var now = DateTime.Now;
            if (now.DayOfWeek == DayOfWeek.Sunday || now.DayOfWeek == DayOfWeek.Saturday)
            {
                Dispatcher.BeginInvoke(new Action(MinimizeWindow));
            }
            else
            {
                switch (now.Hour)
                {
                    case 17: // 5pm
                        Dispatcher.BeginInvoke(new Action(ToggleRedButton));
                        break;
                    case 3: // 3am
                        Dispatcher.BeginInvoke(new Action(ToggleGreenButton));
                        break;
                }

                if (now.Hour < 18 && now.Hour > 2)
                {
                    Dispatcher.BeginInvoke(new Action(MinimizeWindow));
                }
                else
                {
                    if (GreenButton.Visibility == Visibility.Collapsed)
                    {
                        Dispatcher.BeginInvoke(new Action(RestoreWindow));
                    }
                }

            }
        }
    }
}
